<?php defined('SYSPATH') or die('No direct script access.');

    /**
     * Created by PhpStorm.
     * Author: lh
     * Time: 2020/07/13
     * Desc: 产品收藏相关处理
     */
    class Controller_Pc_Api_V2_Collect extends Controller_Pc_Api_V2_Base
    {

        private $result;

        /**
         * 验证类
         * @var Request_Api_V2_Collect
         */
        private $validation;

        /**
         * 服务类
         * @var Service_Api_Collect
         */
        private $service;

        public function before()
        {
            parent::before();
            $this->result = array(
                'status' => true,//默认状态为成功
                'msg' => '操作成功',//默认的提示消息
                'data' => array()
            );
            $this->validation = Request_Api_V2_Factory::create('collect',$this->request_params);
            $this->service = new Service_Api_Collect();
        }
        public function after()
        {
            $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
        }

        /**
         * @function  检查产品是否已收藏
         */
        public function action_is_collect()
        {
            try{

                $this->validation->is_collect()->check();
                //类型id
                $type_id = (int)$this->request_params['type_id'];
                //产品id
                $id = (int)$this->request_params['id'];
                $collect = $this->service->is_collect($type_id,$this->mid,$id);
                $this->result['data']['is_collect'] = $collect ? 1 : 0;

            } catch (Exception $e) {
                $this->result = array('status' => false, 'msg' => $e->getMessage());
            }

        }
        /**
         * 更改收藏
         *
         */
        public function action_modify()
        {
            try{
                $this->validation->modify()->check();
                $this->result = $this->service->modify($this->request_params['type_id'],$this->mid,$this->request_params['id']);
            } catch (Exception $e) {
                $this->result = array('status' => false, 'msg' => $e->getMessage());
            }

        }

        /**
         * 我的收藏列表
         * @throws ReflectionException
         */
        public function action_get_collect_list()
        {
            try{
                $this->validation->get_collect_list()->check();
                //页码
                $page = (int)$this->request_params['page'];
                //每页数量
                $pageSize = (int)$this->request_params['pagesize'];
                $this->result['data'] = $this->service->get_collect_list($this->mid,$page,$pageSize);
            } catch (Exception $e) {
                $this->result = array('status' => false, 'msg' => $e->getMessage());
            }
        }

        /**
         * 获取我的收藏数量
         */
        public function action_get_collect_num()
        {
            try{
                $this->validation->check();
                $this->result['data']['num'] = $this->service->get_collect_num($this->mid);
            } catch (Exception $e) {
                $this->result = array('status' => false, 'msg' => $e->getMessage());
            }
        }

        /**
         * 删除收藏
         */
        public function action_del_collect_product()
        {
            try{
                $this->validation->del_collect_product()->check();
                $collect_ids = $this->request_params['ids'];
                $flag = $this->service->del_collect_product($collect_ids, $this->mid);
                $this->result['msg'] = $flag ? '操作成功' : '操作失败';
                $this->result['status'] = $flag ? true : false;
            } catch (Exception $e) {
                $this->result = array('status' => false, 'msg' => $e->getMessage());
            }
        }

    }